home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / l33t_liek_jeff_k / leet source / HQHackMac.m < prev    next >
Text File  |  2000-06-23  |  4KB  |  83 lines

  1. // HQHackMac.m
  2. //
  3. // You may freely copy, distribute, and reuse the code in this example.
  4. // Apple disclaims any warranty of any kind, expressed or  implied, as to its
  5. // fitness for any particular use.
  6.  
  7. #import "HQHackMac.h"
  8. #import "HQInfoPanelController.h"
  9. #import "HQPreferencesController.h"
  10. #import "HQApplication.h"
  11.  
  12. BOOL gOn = false;
  13.  
  14. // If you want the bundle to insert a menu into apps that load it, uncomment the next line.  Otherwise, comment it out.
  15. #define ENABLE_MENU
  16.  
  17. @implementation HQHackMac
  18.  
  19. // ************************* Set up *************************
  20.  
  21. + (void)load {
  22.     [HQApplication doNSAppInstancePose];
  23. #ifdef ENABLE_MENU
  24.     // Arrange to install the menu.  We will try at least three different ways of getting notified.
  25.     // We try to install the menu at willFinishLaunching, but we will try again at didFinishLaunching just in case willFinishLaunching passed us by.
  26.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationWillFinishLaunchingNotification object:NSApp];
  27.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationDidFinishLaunchingNotification object:NSApp];
  28.     // Finally, there may be cases where the didFinishLaunching has already happened by the time we get loaded.  So we'll also try it with a timer too.
  29.     [self performSelector:@selector(tryToInstallMenu:) withObject:nil afterDelay:0.0];
  30. #endif
  31. }
  32.  
  33. // ********************** Install extras menu **********************
  34.  
  35. + (void)tryToInstallMenu:(NSNotification *)notification {
  36.     static BOOL alreadyInstalled = NO;
  37.     NSMenu *mainMenu = nil;
  38.  
  39.     // We only try once, but that one time needs to be after there is a main menu.  If there is none now, we will pass and hope this method gets called again later.
  40.     if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
  41.         NSMenu *insertIntoMenu = nil;
  42.         NSMenuItem *item;
  43.         unsigned insertLoc = NSNotFound;
  44.         NSBundle *bundle = [NSBundle bundleForClass:[HQHackMac class]];
  45.         // Succeed or fail, we do not try again.
  46.         alreadyInstalled = YES;
  47.  
  48.         item = (([mainMenu numberOfItems] > 0) ? [mainMenu itemAtIndex:0] : nil);
  49.         if (item && [item hasSubmenu]) {
  50.             insertIntoMenu = [item submenu];
  51.             item = [insertIntoMenu itemWithTitle:NSLocalizedStringFromTableInBundle(@"Services", @"Localizable", bundle, @"Title of Services menu item")];
  52.             if (!item) {
  53.                 // MF: Try again with unlocalized string "Text".  This is a backstop that assumes that any app not localized into the user's chosen language was probably developed in English...
  54.                 item = [insertIntoMenu itemWithTitle:@"Services"];
  55.             }
  56.             if (item) {
  57.                 insertLoc = [[insertIntoMenu itemArray] indexOfObjectIdenticalTo:item] + 1;
  58.             } else {
  59.                 // Just insert at head of menu.  This is kind of yucky...
  60.                 insertLoc = 0;
  61.             }
  62.         } 
  63.  
  64.         if (insertIntoMenu) {
  65.             item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"l33tification", @"Localizable", bundle, @"Title of leet menu") action:@selector(leetAction:) keyEquivalent:@"" atIndex:insertLoc];
  66.             [item setTarget:self];
  67.         }
  68.     }
  69. }
  70.  
  71. + (void)leetAction:(id)sender {
  72.     gOn = !gOn;
  73. }
  74.  
  75. + (BOOL)validateMenuItem:(NSMenuItem *)item {
  76.     if ([item action] == @selector(leetAction:)) {
  77.         [item setState:gOn];
  78.     }
  79.     return YES;
  80. }
  81.  
  82. @end
  83.